import {
  Component,
  ViewChild,
  Injector,
  Output,
  EventEmitter,
  ElementRef,
  ViewEncapsulation
} from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
  WlReleaseStockBillListDto,
  MaterialIntoServiceProxy,
  WlReleaseStockBillEditDto,
  ComboboxItemDto,
  IntoWarehouseInput,
  LocationServiceProxy,
  LocationDto,
  MaterialChargeServiceProxy,
  MaterialChargeDetailListDto,
  GetPositionMaterials
} from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
import { LazyLoadEvent } from 'primeng/primeng';

@Component({
  selector: 'updateMaterialPositionModal',
  templateUrl: './update-material-position-modal.component.html',
  encapsulation: ViewEncapsulation.None
})
export class UpdateMaterialPositionModalComponent extends AppComponentBase {
  @ViewChild('actionModal') modal: ModalDirective;

  @ViewChild('areaCombobox') areaCombobox: ElementRef;
  @ViewChild('shelfCombobox') shelfCombobox: ElementRef;
  @ViewChild('positionCombobox') positionCombobox: ElementRef;

  @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

  active = false;
  saving = false;
  details = new Array<GetPositionMaterials>();
  selectedDetail = new GetPositionMaterials();
  // releaseStockBill = new WlReleaseStockBillEditDto();
  event: LazyLoadEvent;
  areas = new Array<LocationDto>();
  shelfs = new Array<LocationDto>();
  positions = new Array<LocationDto>();
  id = '';
  areaId = 0;
  shelfId = 0;
  positionId = 0;
  mid = '';

  constructor(
    injector: Injector,
    private _materialServiceProxy: MaterialChargeServiceProxy,
    private _locationServiceProxy: LocationServiceProxy
  ) {
    super(injector);
  }

  show(
    id: string,
    // details: Array<MaterialChargeDetailListDto>,
    event?: LazyLoadEvent
  ): void {
    this.active = true;
    this.id = id;
    this.event = event;
    this._materialServiceProxy
      .getUpdateMaterial(this.id)
      .pipe(
        finalize(() => {
          this.saving = false;
        })
      )
      .subscribe(result => {
        this.details = result;
        if (this.details.length) {
          this.selectedDetail = this.details[0];
        }
        //console.log(result);
        //this.getposition(this.details[0].id);
      });
    setTimeout(() => {
      this.modal.show();
    }, 0);
  }

  // getposition(id: string) {
  //   this._materialServiceProxy
  //     .getMaterialChargePositionForEdit(id)
  //     .subscribe(result => {
  //       this.areaId = result.areaId;
  //       this.shelfId = result.shelfId;
  //       this.positionId = result.positionId;
  //     });
  // }

  // loadShelfs() {
  //   this.shelfId = 0;
  //   this.positionId = 0;

  //   this._locationServiceProxy
  //     .getShelvesByArea(this.areaId)
  //     .subscribe(result => {
  //       this.shelfs = result.items;
  //       setTimeout(() => {
  //         $(this.shelfCombobox.nativeElement).selectpicker('refresh');
  //       }, 0);
  //     });
  // }

  // loadPositions() {
  //   this.positionId = 0;

  //   this._locationServiceProxy
  //     .getPositionsByShelf(this.shelfId)
  //     .subscribe(result => {
  //       this.positions = result.items;
  //       setTimeout(() => {
  //         $(this.positionCombobox.nativeElement).selectpicker('refresh');
  //       }, 0);
  //     });
  // }

  save(): void {
    this.mid = this.selectedDetail.id;
    if (!this.positionId) {
      this.notify.error('请选择一个位置');
      return;
    }
    this.saving = true;
    this._materialServiceProxy
      .updatePosition(this.mid, this.positionId)
      .pipe(
        finalize(() => {
          this.saving = false;
        })
      )
      .subscribe(result => {
        this.notify.info('修改成功');
        this.close();
        this.modalSave.emit(this.event);
      });
  }

  close(): void {
    this.saving = false;
    this.modal.hide();
  }
}
